package homewoek12月22号.demo1.d4;

import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo1 {
    static ArrayList<Student> list1 = new ArrayList<>();
    static ArrayList<Course> list2 = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        Element root = new SAXReader().read("src/com/czxy/demo1/d4/student.xml").getRootElement();
        Element root1 = new SAXReader().read("src/com/czxy/demo1/d4/course.xml").getRootElement();
        List<Element> slist = root.elements("student");
        List<Element> clist = root1.elements("course");
        for (Element element : slist) {
            list1.add(new Student(element.attributeValue("id"),element.attributeValue("name"),element.attributeValue("age"),element.attributeValue("course")));
        }
        for (Element element : clist) {
            list2.add(new Course(element.attributeValue("id"),element.elementText("name")));
        }
        run();
        run1();
        run2();
        run3();
    }

    private static void run3() {
        for (Student student : list1) {
            for (Course c : student.getList()) {
                if (c.getYd().equals("羽毛球")&&student.getAge().equals("18")){
                    System.out.println(student);
                }
            }
        }
    }

    private static void run() {
        System.out.println(list1);
        System.out.println(list2);
        for (Student s : list1) {
            for (Course c : list2) {
                if (s.getCourse().equals(c.getId())){
                    s.getList().add(c);
                }
            }
        }
    }

    private static void run2() {
        HashMap<String, Integer> map = new HashMap<>();
        for (Student s : list1) {
            for (Course c : s.getList()) {
                map.put(c.getYd(),0);
            }
        }
        for (Student s : list1) {
            for (Course c : s.getList()) {
                if (c.getId().equals(s.getCourse())){
                    map.put(c.getYd(),map.get(c.getYd())+1);
                }
            }
        }
        int max=0;
        for (Map.Entry<String, Integer> s : map.entrySet()) {
            if (s.getValue()>max){
                max=s.getValue();
            }
        }
        System.out.print("人数最多的课程名是：");
        for (Map.Entry<String, Integer> s : map.entrySet()) {
            if (s.getValue()==max){
                System.out.print(s.getKey()+" ");
            }
        }
        System.out.println();
    }

    private static void run1() {
        for (Student student : list1) {
            if (student.getList().get(0).getYd().equals("羽毛球")){
                System.out.println(student.getName()+","+student.getId()+","+student.getCourse()+","+student.getAge());
            }
        }
    }
}
